package com.xw.bo.ks.aspect;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xw.bo.ks.callback.BaseBeanCallBack;
import com.xw.bo.ks.client.RedisClient;
import com.xw.bo.ks.entity.CacheControllerInfo;
import com.xw.bo.ks.entity.common.CodeCommon;
import com.xw.bo.ks.service.CacheControllerInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author： XW -> ~
 * @create： 2024/6/6 16:47
 */
@Slf4j
@Service
public class CacheControllerCodeInterfaceImpl implements CacheControllerCodeInterface {

    @Value("${cache.controller.open:false}")
    private boolean cacheOpen;

    @Value("${cache.controller.uri:redis}")
    private String cacheType;


    @Resource
    private RedisClient redisClient;

    @Resource
    private CacheControllerInfoService cacheControllerInfoService;

    /**
     * 缓存内容
     * key=uri+请求参数
     */
    Map<String, Object> objectMap = new HashMap<>();

    /**
     * 是否开启缓存
     * key=uri
     */
    Map<String, Integer> uriCache = new HashMap<>();

    /**
     * 缓存时长
     * key=uri
     */
    Map<String, Long> cacheTimeMap = new HashMap<>();

    @Override
    public boolean isCache(String uri) {
        if (!cacheOpen) {
            return false;
        }
        if (!uriCache.containsKey(uri)) {
            return false;
        }
        return CodeCommon.ENABLE_OPEN.equals(uriCache.get(uri));
    }

    @Override
    public boolean addCache(String uri, Long cacheTime) {
        uriCache.put(uri, CodeCommon.ENABLE_OPEN);
        cacheTimeMap.put(uri, cacheTime);
        return true;
    }

    @Override
    public boolean openCloseCache(String uri, Integer enable) {
        uriCache.put(uri, enable);
        return true;
    }

    @Override
    public Object backResult(String key) {
        if (CodeCommon.CACHE_TYPE_REDIS.equals(cacheType)) {
            return redisClient.get(key);
        } else if (CodeCommon.CACHE_TYPE_MYSQL.equals(cacheType)) {
            // 暂时不缓存
            return null;
        }
        return objectMap.get(key);
    }

    @Override
    public void saveResult(String uri, String key, Object object) {
        if (StringUtils.isEmpty(key)) {
            return;
        }

        if (null == object) {
            return;
        }

        if (CodeCommon.CACHE_TYPE_REDIS.equals(cacheType)) {
            redisClient.set(key, object, cacheTimeMap.getOrDefault(uri, 7200L));
        } else if (CodeCommon.CACHE_TYPE_MYSQL.equals(cacheType)) {
            // 暂时不缓存
            return;
        }

        objectMap.put(key, object);
    }

    @Override
    public void readMysqlCache() {
        LambdaQueryWrapper<CacheControllerInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        cacheControllerInfoService.loopDsAccessTokens(lambdaQueryWrapper, new BaseBeanCallBack<CacheControllerInfo>() {
            @Override
            public void callBack(CacheControllerInfo cacheControllerInfo) {
                uriCache.put(cacheControllerInfo.getUri(), Integer.valueOf(cacheControllerInfo.getEnableType()));
                cacheTimeMap.put(cacheControllerInfo.getUri(), Long.valueOf(cacheControllerInfo.getCacheTime()));
            }
        });
    }

}
